
Widget = {};

------------------------------------------
--SDL2
if(ui:isPlatform("SDL2"))then
function Widget.SetSize(self,w,h)
    if(self.sdl2)then
        
    end
end

widgetState = {
    rendering = 1,
}

function Widget.LockRender(w)
    if(w.sdl2==nil)then
        w.sdl2 = {};
        w.sdl2.name = allocName("widget");
        --print(w.sdl2.name);
        --w.sdl2.width = w.width;
        --w.sdl2.height = w.height;
        w.state = "rendering";
        w.sdl2.srcRect = SDL_calloc(4*4,3);
        w.sdl2.dstRect = dpointer(w.sdl2.srcRect,4*4);
    end
    --if(widgetState[w.state]==widgetState.rendering)then
    if(w.state or w.lowRender)then
        local width = w.sdl2.width;
        local height = w.sdl2.height;
        if(width==nil or width<w.width or height==nil or height<w.height)then
            createCanvas(w.sdl2.name,w.width,w.height);
            print(w.sdl2.name);
            w.sdl2.width = w.width;
            w.sdl2.height = w.height;

            dw(w.sdl2.srcRect,8,4,w.width);
            dw(w.sdl2.srcRect,12,4,w.height);
            
            dw(w.sdl2.dstRect,8,4,w.width);
            dw(w.sdl2.dstRect,12,4,w.height);
        end
        
        dw(w.sdl2.dstRect,0,4,w.x);
        dw(w.sdl2.dstRect,4,4,w.y);
        
        w.qrender = function(self)
            qdraw(w.sdl2.name,w.sdl2.srcRect,w.sdl2.dstRect);
        end

        w.sdl2.lastRenderTarget = setRenderTarget(w.sdl2.name);
        w.state = nil;
        return true;
    end
    --if(w.sdl2.name and w.lowRender==nil)then

    if(w.sdl2.name)then
        --w.lowRender = nil;
        --绘制到主窗体
        if(lluiVersion==nil)then
            --unlockRender
            --w:qrender();
            draw(w.sdl2.name,w.x,w.y,w.width,w.height);
            --renderPresent();
        else
            if(w.isWindow)then
                draw(w.sdl2.name,0,0,w.width,w.height);
            else
                draw(w.sdl2.name,w.x,w.y,w.width,w.height);
            end
        end
    end
end

function Widget.UnlockRender(w)
    if(w.sdl2.lastRenderTarget)then 
        --renderPresent();
        setRenderTarget(w.sdl2.lastRenderTarget);
    end
    if(w.sdl2.name)then
        --绘制到主窗体
        --绘制到主窗体
        if(lluiVersion==nil)then
            --unlockRender
            --w:qrender();
            draw(w.sdl2.name,w.x,w.y,w.width,w.height);
            --renderPresent();
        else
            if(w.isWindow)then
                draw(w.sdl2.name,0,0,w.width,w.height);
            else
                draw(w.sdl2.name,w.x,w.y,w.width,w.height);
            end
        end
        if(w.isWindow)then
            renderPresent();
        end
    end
end

end
--SDL2 
------------------------------------------------

Widget.__index = Widget;


setmetatable(Widget,{
__call = function()
    local w = {};
    w.x = 0;
    w.y = 0;
    w.width = 10;
    w.height = 10;
    w.shown = 1;
    w.subWidgets = {};
    setmetatable(w, Widget);
    return w;
    end
});

function Widget.render(w)
    --锁定渲染
    if(w:LockRender())then
        local backgroundcolor = w.backgroundcolor;
        if(backgroundcolor)then
            drawRect(0,0,w.width,w.height,backgroundcolor,true);
        end
        for k,v in ipairs(w.subWidgets)do
            if(v.shown)then
                v:render();
            end
        end
        w:UnlockRender();
    end
end

function Widget.add(self,w)
    w.parent = self;
    table.insert(self.subWidgets,w);
end

function Widget.event(w,event)
    local state;
    for k,v in ipairs(w.subWidgets)do
        if(v.event)then
            state = v:event(event);
            if(state~=0 or state==true)then
                --break;
            end
            --print(v._type);
        end
    end
    if(state==nil)then
        local name = event.name;
        if(w._type)then
            print(w._type,name,w[name]);
        end
        if(w[name])then
            return w[name](w,event);
        end
    end
end
